package com.koron.order.linepatrol.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.bean.scheduleShift.ScheduleShift;
import com.koron.bean.scheduleShift.ShiftStaff;
import com.koron.bean.system.org.OrgBean;
import com.koron.bean.system.staff.Constant;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.OrgCacheUtils;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.linepatrol.base.bean.po.PatrolPo;
import com.koron.order.linepatrol.base.bean.po.PatrolUserPo;
import com.koron.order.linepatrol.base.bean.query.PatrolQuery;
import com.koron.order.linepatrol.base.mapper.PatrolMapper;
import com.koron.order.linepatrol.base.service.PatrolService;
import com.koron.order.linepatrol.base.service.PatrolUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 巡视工单-基础信息(Patrol)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:32:38
 */
@Slf4j
@Service("linePatrolService")
public class PatrolServiceImpl implements PatrolService {

    @Autowired
    PatrolUserService linePatrolUserService;
    @Autowired
    OrgCacheUtils orgCacheUtils;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolPo patrolPo) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        if (StringUtils.isEmpty(patrolPo.getId())) {
            patrolPo.setId(CodeTools.getCode32());
        }

        if (StringUtils.isEmpty(patrolPo.getCode())) {
            OrgBean orgBean = orgCacheUtils.getOrgById(patrolPo.getBelongDeptId());
            if(orgBean!=null){
                String simpleEn = orgBean.getPrefix();
                patrolPo.setCode(CommonCodeTools.getOrderCode(getCodePrefix(patrolPo),simpleEn));
            }else {
                patrolPo.setCode(CommonCodeTools.getNormalOrderCode(getCodePrefix(patrolPo)));
            }
        }

        return mapper.insertOrUpdate(patrolPo);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolPo> patrolList) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        return mapper.insertOrUpdateBatch(patrolList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolPo patrol) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        return mapper.insert(patrol);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolPo> patrolList) {
        if (CollectionUtils.isEmpty(patrolList)) {
            return 0;
        }
        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        return mapper.insertBatch(patrolList);
    }

    private OrderCodeEnum getCodePrefix(PatrolPo patrolPo) {
        if ("other".equals(patrolPo.getOrderSource())) {
            return OrderCodeEnum.XLQTXS;
        }
        if ("timing".equals(patrolPo.getPlanCategory())) {
            if ("scene".equals(patrolPo.getPlanPatrolWay())) {
                return OrderCodeEnum.DQXCXS;
            } else if ("video".equals(patrolPo.getPlanPatrolWay())) {
                return OrderCodeEnum.DQSPXS;
            }
        } else if ("special".equals(patrolPo.getPlanCategory())) {
            if ("scene".equals(patrolPo.getPlanPatrolWay())) {
                return OrderCodeEnum.TSXCXS;
            } else if ("video".equals(patrolPo.getPlanPatrolWay())) {
                return OrderCodeEnum.TSSPXS;
            }
        }
        throw new IllegalArgumentException("改巡视类型不存在");
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolPo patrol) {
        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);
        return mapper.update(patrol);
    }

    @Override
    @TaskAnnotation("updateStatus")
    public int updateStatus(SessionFactory factory, String id, String status) {
        PatrolPo patrolPo = new PatrolPo();
        patrolPo.setId(id);
        patrolPo.setStatus(status);
        return update(factory, patrolPo);
    }

    @Override
    @TaskAnnotation("updatePrepare")
    public int updatePrepare(SessionFactory factory, String id, String prepare) {
        PatrolPo patrolPo = new PatrolPo();

        PatrolPo patrolPo1 = queryById(factory, id);
        if (patrolPo1.getStatus() == null || "WAIT_PATROL".equals(patrolPo1.getStatus())) {
            patrolPo.setStatus("IN_PATROL");
        }
        patrolPo.setId(id);
        patrolPo.setPrepare(prepare);
        return update(factory, patrolPo);
    }

    @Override
    @TaskAnnotation("updatePatrolStaff")
    public int updatePatrolStaff(SessionFactory factory, ScheduleShift scheduleShift) {

        List<ShiftStaff> staffs = scheduleShift.getStaffs();

        Assert.notEmpty(staffs, "人员列表不能为空");

        //转换计划及交接班 字典
        String key = getKey(scheduleShift.getStaffType());

        scheduleShift.setStaffType(key);

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        // 查询所有符合条件的巡视工单
        List<PatrolPo> patrolList = mapper.queryPatrolByCondition(scheduleShift);

        if (!CollectionUtils.isEmpty(patrolList)) {
            log.info("查询到{}条需要指定人员的巡视工单", patrolList.size());
            List<PatrolUserPo> patrolUserPos = new ArrayList<>();
            for (PatrolPo patrol : patrolList) {
                String patrolId = patrol.getId();
                //根据 patrolId 更新巡视人员
                for (int i = 0; i < staffs.size(); i++) {
                    PatrolUserPo patrolUserPo = new PatrolUserPo();
                    patrolUserPo.setWorker(staffs.get(i).getStaffBy());
                    patrolUserPo.setWorkerName(staffs.get(i).getStaffName());
                    patrolUserPo.setSort(i);
                    patrolUserPo.setPatrolId(patrolId);
                    patrolUserPo.setPatrolDeptId(scheduleShift.getDeptId());
                    patrolUserPo.setPatrolDeptName(scheduleShift.getDeptName());
                    patrolUserPos.add(patrolUserPo);
                }
            }
            return linePatrolUserService.insertOrUpdateBatch(factory, patrolUserPos);
        }
        log.info("未找到需要指定人员的巡视工单");
        return 0;
    }

    public String getKey(String staffType) {
        Map<String, String> map = Constant.StaffingWay.map;

        Set<String> keys = map.keySet();

        for (String key : keys) {
            String value = map.get(key);
            if (value.equals(staffType)) {
                return key;
            }
        }
        return null;
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolPo queryById(SessionFactory factory, String id) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolPo> queryList(SessionFactory factory, PatrolQuery query) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        List<PatrolPo> list = mapper.queryList(query);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolQuery query, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        List<PatrolPo> list = mapper.queryList(query);

        return new PageInfo<PatrolPo>(list);
    }
    @Override
    public int updatePatrolInsertFlag(SessionFactory factory, PatrolPo patrolPo) {

        PatrolMapper mapper = factory.getMapper(PatrolMapper.class);

        return mapper.updatePatrolInsertFlag(patrolPo);
    }

}